package com.yonyougov.cruxtestcase.component;

import com.yonyougov.cruxtestcase.testcase.allbasequery.dto.ServiceEntityDto;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Component
public class CommonQuery {
    private final RestTemplate restTemplate;

    public CommonQuery(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 获取每个服务所有的实体类
     * @param token 凭证
     */
    public List<ServiceEntityDto> getEntityByServiceId(String url, String token){
        HttpHeaders headers=new HttpHeaders();
        headers.set("Authorization","Bearer " + token);
        HttpEntity entity = new HttpEntity<>(headers);
        return restTemplate.exchange(url, HttpMethod.GET, entity, new ParameterizedTypeReference<ArrayList<ServiceEntityDto>>() {
        }).getBody();
    }

    /**
     *通用查询
     * @param token 凭证
     */
    public ResponseEntity commonQuery1(String commonQueryUrl, String token){
        HttpHeaders headers=new HttpHeaders();
        headers.set("Authorization","Bearer " + token);
        HttpEntity entity = new HttpEntity<>(headers);
        return restTemplate.exchange(commonQueryUrl, HttpMethod.GET, entity, String.class);
    }
}
